<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Xls extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model("ciq_m");
	}
	
	function Get(){
		$this->load->helper('url');
		$ci=$this->input->get("ci");
		$res=$this->ciq_m->get($ci);
		echo json_encode(array("flag"=>count($res)>0?"1":"0","info"=>$res));
	}
	
	function Add(){
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory');
		$this->load->helper('url');
		
		if(isset($_FILES['file'])){
			$data['has_file']="has";
			preg_match('|\.(\w+)$|', $_FILES["file"]["name"], $ext);
			$file_name="upload/data" . $ext[0];
			if(file_exists($file_name)){
				unlink($file_name);
			}
			move_uploaded_file($_FILES["file"]["tmp_name"],$file_name);
				
			$objReader =IOFactory::createReader('Excel5');//use excel2007 for 2007 format
			$objPHPExcel = $objReader->load($file_name);//加载目标Excel
			$sheet = $objPHPExcel->getSheet(0);//读取第一个sheet
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			$succ_result=$error_result=0;//设置导入成功和失败的总数为0
				
			$data=array();
			for ($i=2;$i<=$highestRow;$i++){
				$strExcel='';
				for ($j="A";$j<=$highestColumn;$j++){
					$strExcel .= $objPHPExcel->getActiveSheet()->getCell("$j$i")->getValue();
				}
		
				$ci = $objPHPExcel->getActiveSheet()->getCell("B$i")->getValue();
				$lac = $objPHPExcel->getActiveSheet()->getCell("A$i")->getValue();
				$cp_name = $objPHPExcel->getActiveSheet()->getCell("C$i")->getValue();
				$bst_name = $objPHPExcel->getActiveSheet()->getCell("D$i")->getValue();
				$bst_addr = $objPHPExcel->getActiveSheet()->getCell("E$i")->getValue();
				$rru1_area = $objPHPExcel->getActiveSheet()->getCell("F$i")->getValue();
				$rru2_area = $objPHPExcel->getActiveSheet()->getCell("G$i")->getValue();
		
				array_push($data,array("ci" =>$ci ,"lac" =>$lac ,"cp_name" =>$cp_name ,"bst_name" =>$bst_name ,"bst_addr" =>$bst_addr ,"rru1_area" =>$rru1_area ,"rru2_area" =>$rru2_area ));
			}
			$num=$this->ciq_m->add($data);
			$data['num']=$num;
			$data['has_file']="y";
		}else{
			$data['has_file']="n";
		}
		
		$this->load->view('add',$data);
	}
	
	function Update(){
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory');
		$this->load->helper('url');
		
		if(isset($_FILES['file'])){
			$data['has_file']="has";
			preg_match('|\.(\w+)$|', $_FILES["file"]["name"], $ext);
			$file_name="upload/data" . $ext[0];
			if(file_exists($file_name)){
				unlink($file_name);
			}
			move_uploaded_file($_FILES["file"]["tmp_name"],$file_name);
			
			$objReader =IOFactory::createReader('Excel5');//use excel2007 for 2007 format
			$objPHPExcel = $objReader->load($file_name);//加载目标Excel
			$sheet = $objPHPExcel->getSheet(0);//读取第一个sheet
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			$succ_result=$error_result=0;//设置导入成功和失败的总数为0
			
			$data=array();
			for ($i=2;$i<=$highestRow;$i++){
				$strExcel='';
				for ($j="A";$j<=$highestColumn;$j++){
					$strExcel .= $objPHPExcel->getActiveSheet()->getCell("$j$i")->getValue();
				}
				
				$ci = $objPHPExcel->getActiveSheet()->getCell("B$i")->getValue();
				$lac = $objPHPExcel->getActiveSheet()->getCell("A$i")->getValue();
				$cp_name = $objPHPExcel->getActiveSheet()->getCell("C$i")->getValue();
				$bst_name = $objPHPExcel->getActiveSheet()->getCell("D$i")->getValue();
				$bst_addr = $objPHPExcel->getActiveSheet()->getCell("E$i")->getValue();
				$rru1_area = $objPHPExcel->getActiveSheet()->getCell("F$i")->getValue();
				$rru2_area = $objPHPExcel->getActiveSheet()->getCell("G$i")->getValue();
				
				array_push($data,array("ci" =>$ci ,"lac" =>$lac ,"cp_name" =>$cp_name ,"bst_name" =>$bst_name ,"bst_addr" =>$bst_addr ,"rru1_area" =>$rru1_area ,"rru2_area" =>$rru2_area ));
			}
			$num=$this->ciq_m->update($data);
			$data['num']=$num;
			$data['has_file']="y";
		}else{
			$data['has_file']="n";
		}
		
		$this->load->view('update',$data);
	}
}
?>